package com.cooba.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.DatePicker;
import android.widget.TextView;

import com.cooba.R;
import com.cooba.views.choicecity.CityPicker;

/**
 * Created by Administrator on 2016/6/15.
 */
public class DatePickerDialog extends Dialog implements
        OnClickListener ,DatePicker.OnDateChangedListener{
    private int year;
    private int month;
    private int day;

    private static final String YEAR = "year";
    private static final String DAY = "day";
    private static final String MONTH = "month";


    int layoutRes;// 布局文件
    Context context;
    private TextView btnCancel;
    private OnDateSetListener listener;
    private TextView btnComfirm;
    private DatePicker datepicker;

    @Override
    public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        if (view.getId() == R.id.datePicker) {
            datepicker.init(year, monthOfYear, dayOfMonth, this);
        }
    }


    /**
     * The callback used to indicate the user is done filling in the date.
     */
    public interface OnDateSetListener {

        /**
         * @param view
         *            The view associated with this listener.
         * @param year
         *            The year that was set.
         * @param monthOfYear
         *            The month that was set (0-11) for compatibility with
         *            {@link java.util.Calendar}.
         * @param dayOfMonth
         *            The day of the month that was set.
         */
        void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth);
    }


    public DatePickerDialog(Context context) {
        super(context);
        this.context = context;
    }

    /**
     * 自定义布局的构造方法
     *
     * @param context
     * @param resLayout
     */
    public DatePickerDialog(Context context, int resLayout) {
        super(context);
        this.context = context;
        this.layoutRes = resLayout;
    }

    /**
     * 自定义主题及布局的构造方法
     *
     * @param context
     * @param theme
     * @param resLayout
     */
    public DatePickerDialog(Context context, int theme, int resLayout,int year,int month,int day,
                            OnDateSetListener listener) {
        super(context, theme);
        this.context = context;
        this.layoutRes = resLayout;
        this.listener = listener;
        this.year=year;
        this.month=month;
        this.day=day;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(layoutRes);

        // 根据id在布局中找到控件对象
        btnComfirm = (TextView) findViewById(R.id.date_comfirm_btn);
        btnCancel = (TextView) findViewById(R.id.date_cancel_btn);
        datepicker = (DatePicker) findViewById(R.id.datePicker);
        // 为按钮绑定点击事件监听器
        btnComfirm.setOnClickListener(this);
        btnCancel.setOnClickListener(this);
        datepicker.init(year,month,day,this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.date_comfirm_btn:
                this.dismiss();
                tryNotifyDateSet();
                break;

            case R.id.date_cancel_btn:
                this.dismiss();
                break;
        }
    }
    private void tryNotifyDateSet() {
        if (listener != null) {
            datepicker.clearFocus();
            listener.onDateSet(datepicker, datepicker.getYear(), datepicker.getMonth(), datepicker.getDayOfMonth());
        }
    }

    @Override
    protected void onStop() {
        // tryNotifyDateSet();
        super.onStop();
    }

}
